/*
 * HAPSIEGestionDeAreas.java
 *
 * Created on 23/01/2012, 07:41:32 PM
 */
package interfaz.gestionAreas;

import interfaz.Intro.MenuPrincipal;
import java.awt.Dimension;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojos.Areas;


/**
 *
 * @author Erika Berenice Hernandez Angulo
 */
public  class GestionDeAreas extends javax.swing.JDialog {

   public List listAreas;
   public DefaultTableModel modelo;
   int filas = 0;
   
   /** Creates new form HAPSIEGestionDeAreas */
   public GestionDeAreas(java.awt.Dialog parent) {
        super(parent);
        initComponents();
        iniciaTabla();
        setSize(new Dimension(800,550));
    }

   private void iniciaTabla(){
       Areas actual;
       listAreas = MenuPrincipal.manAreas.getFullAreas();
       modelo = (DefaultTableModel) tablaDeAreas.getModel();
       for(int i = 0 ; i < listAreas.size(); i++) {
           actual = (Areas) listAreas.get(i);
           Object [] fil = new Object[2];
           fil[0] = actual.getNombreArea();
           fil[1] = actual.getEncargado();
           modelo.addRow(fil);
       }
       tablaDeAreas.setModel(modelo);
   }
      
  public void actualizaTabla(){
      tablaDeAreas.setModel(new DefaultTableModel());
      iniciaTabla();
  }
 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaDeAreas = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        botonRegresaMenuGestionAreas = new javax.swing.JButton();
        botonBorrarMini = new javax.swing.JButton();
        botonEditaMini = new javax.swing.JButton();
        botonAgregarMini = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(GestionDeAreas.class);
        setTitle(resourceMap.getString("gestionDeAreas.title")); // NOI18N
        setName("gestionDeAreas"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tablaDeAreas.setFont(resourceMap.getFont("tablaDeAreas.font")); // NOI18N
        tablaDeAreas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre del Área:", "Encargado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaDeAreas.setName("tablaDeAreas"); // NOI18N
        jScrollPane1.setViewportView(tablaDeAreas);
        tablaDeAreas.getColumnModel().getColumn(0).setResizable(false);
        tablaDeAreas.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tablaDeAreas.columnModel.title0")); // NOI18N
        tablaDeAreas.getColumnModel().getColumn(1).setResizable(false);
        tablaDeAreas.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tablaDeAreas.columnModel.title1")); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        botonRegresaMenuGestionAreas.setFont(resourceMap.getFont("botonRegresaMenuGestionAreas.font")); // NOI18N
        botonRegresaMenuGestionAreas.setText(resourceMap.getString("botonRegresaMenuGestionAreas.text")); // NOI18N
        botonRegresaMenuGestionAreas.setName("botonRegresaMenuGestionAreas"); // NOI18N
        botonRegresaMenuGestionAreas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonRegresaMenuGestionAreasActionPerformed(evt);
            }
        });

        botonBorrarMini.setFont(resourceMap.getFont("botonBorrarMini.font")); // NOI18N
        botonBorrarMini.setText(resourceMap.getString("botonBorrarMini.text")); // NOI18N
        botonBorrarMini.setName("botonBorrarMini"); // NOI18N
        botonBorrarMini.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarMiniActionPerformed(evt);
            }
        });

        botonEditaMini.setFont(resourceMap.getFont("botonEditaMini.font")); // NOI18N
        botonEditaMini.setText(resourceMap.getString("botonEditaMini.text")); // NOI18N
        botonEditaMini.setName("botonEditaMini"); // NOI18N
        botonEditaMini.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEditaMiniActionPerformed(evt);
            }
        });

        botonAgregarMini.setFont(resourceMap.getFont("botonAgregarMini.font")); // NOI18N
        botonAgregarMini.setText(resourceMap.getString("botonAgregarMini.text")); // NOI18N
        botonAgregarMini.setName("botonAgregarMini"); // NOI18N
        botonAgregarMini.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarMiniActionPerformed(evt);
            }
        });

        jLabel2.setIcon(resourceMap.getIcon("jLabel2.icon")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 677, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(botonRegresaMenuGestionAreas)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 494, Short.MAX_VALUE)
                                .addComponent(jLabel2)))
                        .addGap(81, 81, 81)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addComponent(botonAgregarMini, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(botonEditaMini, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(botonBorrarMini, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(380, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(82, 82, 82)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonBorrarMini, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonEditaMini, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonAgregarMini, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonRegresaMenuGestionAreas, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    ((MenuPrincipal) getOwner()).refrescaMenu();
    setVisible(false);
    getOwner().setVisible(true);
}//GEN-LAST:event_formWindowClosing

private void botonRegresaMenuGestionAreasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonRegresaMenuGestionAreasActionPerformed
    ((MenuPrincipal) getOwner()).refrescaMenu();
    setVisible(false);
    getOwner().setVisible(true);
}//GEN-LAST:event_botonRegresaMenuGestionAreasActionPerformed

private void botonBorrarMiniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarMiniActionPerformed
    int indice = -1;
    indice = tablaDeAreas.getSelectedRow();
    if(indice == -1){
        JOptionPane.showMessageDialog(this, "Seleccione el Área a eliminar");
    } else {
        String nombreAreaSeleccionada = (String) tablaDeAreas.getValueAt(indice, 0);
      
        if(nombreAreaSeleccionada == null){
            JOptionPane.showMessageDialog(this, "No hay información en el elemento seleccionado");
            tablaDeAreas.clearSelection();
        }else{ 
            int response =  JOptionPane.showConfirmDialog(this, "¿Está seguro de que desea borrar esta Área? ");
            if(response == JOptionPane.YES_OPTION){
                MenuPrincipal.manAreas.borraAreaPorId(((Areas) listAreas.get(indice)).getIdArea());
                listAreas = MenuPrincipal.manAreas.getFullAreas();
                actualizaTabla();
                tablaDeAreas.clearSelection();
            }
            if(response == JOptionPane.NO_OPTION || response == JOptionPane.CLOSED_OPTION){
                tablaDeAreas.clearSelection();
            }
        }
    }
}//GEN-LAST:event_botonBorrarMiniActionPerformed

private void botonEditaMiniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEditaMiniActionPerformed
    int indice = -1;
    indice = tablaDeAreas.getSelectedRow();
   
    if(indice == -1){
        JOptionPane.showMessageDialog(this, "Seleccionar el Área a editar");
        tablaDeAreas.clearSelection();
    } else {
        editaAreas = new EditaArea(this, indice, listAreas);
        editaAreas.setLocationRelativeTo(null);
        editaAreas.setVisible(true);
        tablaDeAreas.clearSelection();
    }
}//GEN-LAST:event_botonEditaMiniActionPerformed

private void botonAgregarMiniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarMiniActionPerformed
    tablaDeAreas.clearSelection();
    agregaAreas = new AgregaArea(this, listAreas.size());
    agregaAreas.setLocationRelativeTo(null);
    agregaAreas.setVisible(true);
}//GEN-LAST:event_botonAgregarMiniActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAgregarMini;
    private javax.swing.JButton botonBorrarMini;
    private javax.swing.JButton botonEditaMini;
    private javax.swing.JButton botonRegresaMenuGestionAreas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaDeAreas;
    // End of variables declaration//GEN-END:variables


private JDialog   agregaAreas;
private JDialog  editaAreas ;
DefaultTableModel modeloTabla = new DefaultTableModel();
int fila = 1;
     
}
